/**
 * 简化日志记录器
 * 专注于内容分发平台的核心需求
 */

import { LogLevel } from '../types/core';

/**
 * 日志记录器接口
 */
export interface ILogger {
  debug(message: string, ...args: any[]): void;
  info(message: string, ...args: any[]): void;
  warn(message: string, ...args: any[]): void;
  error(message: string, error?: Error | any, ...args: any[]): void;
}

/**
 * 日志配置
 */
export interface ILoggerConfig {
  level?: LogLevel;
  prefix?: string;
  enableConsole?: boolean;
}

/**
 * 简化的日志记录器实现
 */
export class Logger implements ILogger {
  private level: LogLevel;
  private prefix: string;
  private enableConsole: boolean;

  constructor(config: ILoggerConfig = {}) {
    this.level = config.level ?? LogLevel.INFO;
    this.prefix = config.prefix ?? '';
    this.enableConsole = config.enableConsole ?? true;
  }

  debug(message: string, ...args: any[]): void {
    if (this.level <= LogLevel.DEBUG) {
      this.log('DEBUG', message, ...args);
    }
  }

  info(message: string, ...args: any[]): void {
    if (this.level <= LogLevel.INFO) {
      this.log('INFO', message, ...args);
    }
  }

  warn(message: string, ...args: any[]): void {
    if (this.level <= LogLevel.WARN) {
      this.log('WARN', message, ...args);
    }
  }

  error(message: string, error?: Error | any, ...args: any[]): void {
    if (this.level <= LogLevel.ERROR) {
      this.log('ERROR', message, error, ...args);
    }
  }

  private log(level: string, message: string, ...args: any[]): void {
    if (!this.enableConsole) return;

    const timestamp = new Date().toISOString();
    const prefix = this.prefix ? `[${this.prefix}] ` : '';
    const logMessage = `${timestamp} ${level} ${prefix}${message}`;

    switch (level) {
      case 'DEBUG':
        console.debug(logMessage, ...args);
        break;
      case 'INFO':
        console.info(logMessage, ...args);
        break;
      case 'WARN':
        console.warn(logMessage, ...args);
        break;
      case 'ERROR':
        console.error(logMessage, ...args);
        break;
      default:
        console.log(logMessage, ...args);
    }
  }
}

/**
 * 创建日志记录器
 */
export function createLogger(prefix?: string, config?: ILoggerConfig): ILogger {
  return new Logger({
    ...config,
    prefix
  });
}

/**
 * 默认日志记录器
 */
export const defaultLogger = createLogger('ContentFlow');
